package com.lm.activiti;

import com.google.common.base.Joiner;
import com.lm.activiti.dto.Person;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.collections.Lists;

import java.util.List;

/**
 * @author lm_jcf
 * @date 2018/5/7 15:04
 */
public class HelloWorld {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    @Autowired
    ManagementService managementService;
    //    部署流程定义
    @Test
    public void deploymentProcessDefinition(){
       Deployment deployment = processEngine.getRepositoryService()    //与流程定义和部署对象相关的额Service
                    .createDeployment()     //创建一个部署对象
               .name("请假流程3")
               .addClasspathResource("bpmn/helloworld.bpmn")    //加载资源dpmn
               .deploy();   //完成部署

        System.out.println("流程Id"+deployment.getId());
        System.out.println("流程名称"+deployment.getName());

    }


    @Test
    public void startProcessInstance(){
        String processKey = "helloworld";

        ProcessInstance pi = processEngine.getRuntimeService()      //与正在执行的流程实例和执行对象相关的Service
                .startProcessInstanceByKey(processKey);     //使用流程定义的Key启动流程实例，可以使之每次都启动最新版本的流程。

        System.out.println("流程实例ID："+pi.getId());
        System.out.println("流程定义ID："+pi.getProcessDefinitionId());

    }

    @Test
    public void findInvolve(){
        String assignee = "jcf";

        List<HistoricProcessInstance> hiProcInst = processEngine.getHistoryService().createHistoricProcessInstanceQuery()
//                .startedBy(assignee)
                .involvedUser(assignee)
                .orderByProcessInstanceStartTime().desc()
                .list();
        if (hiProcInst != null && hiProcInst.size() >0) {
            for (HistoricProcessInstance task : hiProcInst) {
                System.out.println(task.getId());
                System.out.println(task.getEndTime());
                System.out.println(task.getStartUserId());
                System.out.println("------");
            }
        }
    }

    @Test
    public void test(){
        String taskId = "";
        Task targetTask = processEngine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) processEngine.getRepositoryService()
                .getProcessDefinition(targetTask.getProcessDefinitionId());

    }

    @Test
    public void findMeHandle(){
        String assignee = "小白";

        List<HistoricTaskInstance> hiUserTasks = processEngine.getHistoryService().createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .list();

        if (hiUserTasks != null && hiUserTasks.size() >0) {
            for (HistoricTaskInstance task : hiUserTasks) {
                System.out.println(task.getId());
                System.out.println(task.getEndTime());
                System.out.println(task.getProcessInstanceId());
                System.out.println("------");
            }
        }
    }


    @Test
    public void findMyProcessTask(){
        String assignee = "张三";
        List<Task> list = processEngine.getTaskService()  //与正在执行的任务相关的Service
                .createTaskQuery()  //创建任务查询对象
                /** 查询条件  */
                .taskAssignee(assignee) //指定个人任务对象
//                .taskCandidateUser(taskCandidateUser) //组任务的办理人
//                .processDefinitionId()  //使用流程定义Id查询
//                .processInstanceId()    //使用流程实例ID
//                .executionId()  //使用执行对象id查询
                /** 排序*/
                .orderByTaskCreateTime().asc()  //使用创建时间升序排序
                /** 返回结果集*/
//                .singleResult()   //唯一结果集
//                .count()  //返回结果集的数量
//                .listPage()   //分页查询
                .list();

        if (list != null && list.size() >0){
            for (Task task : list) {
                System.out.println("任务ID:"+task.getId());
                System.out.println("任务名称:"+task.getName());
                System.out.println("任务创建时间:"+task.getCreateTime());
                System.out.println("任务办理人:"+task.getAssignee());
                System.out.println("流程实例ID:"+task.getProcessInstanceId());
                System.out.println("执行对象ID:"+task.getExecutionId());
                System.out.println("流程定义ID:"+task.getProcessDefinitionId());
            }
        }
    }

    @Test
    public void completeMyProcessTask(){
        String taskId = "304";
        processEngine.getTaskService()
                .complete(taskId);
        System.out.println("完成任务");
    }

    public static void main(String[] args) {
        String join = Joiner.on("/").join("a", "a");
        System.out.println(join);

        List<Person> peoples = Lists.newArrayList();
        peoples.add(new Person(1, "王", "12"));
        peoples.add(new Person(2, "江", "22"));
        peoples.add(new Person(3, "李", "22"));
        peoples.add(new Person(4, "d", "22"));

    }
}
